package com.cloudspace.jindun.rongyun;

import com.cloudspace.jindun.config.Configs;
import com.cloudspace.jindun.model.ChannelInfoResult;
import com.cloudspace.jindun.model.ChannelListResult;
import com.cloudspace.jindun.model.ChannelMemberListResult;
import com.cloudspace.jindun.model.ChannelMemberStatusResult;
import com.cloudspace.jindun.model.CommonResult;
import com.cloudspace.jindun.model.DefaultChannelResult;
import com.cloudspace.jindun.model.MicResult;
import com.cloudspace.jindun.network.HttpManager;
import com.cloudspace.jindun.network.api.BaseAPI;
import com.cloudspace.jindun.network.base.ApiCallback;
import com.cloudspace.jindun.network.base.GsonRequest;
import com.cloudspace.jindun.network.base.RequestFactory;
import com.cloudspace.jindun.user.UserSessionManager;

import static com.cloudspace.jindun.R.id.channelId;

/**
 * Created by zengxianhua on 17/1/26.
 */

public class ChannelApi extends BaseAPI{
    //获取频道列表
    public void getChannelList(String tag, ApiCallback<ChannelListResult> callback){
        String url = getBaseChannelRequestUrl().append("line/").append(UserSessionManager.getInstance().getUserId()).toString();

        GsonRequest request = RequestFactory.newInstance()
                .create(url, ChannelListResult.class, callback)
                .setNoCache();
        HttpManager.addRequest(request, tag);
    }

   public void channelIdAvailable(String tag, String channelId, ApiCallback<CommonResult> callback){
       String url = getBaseChannelRequestUrl().append("IsAvailable").toString();

       GsonRequest request = RequestFactory.newInstance()
               .createPost(url, CommonResult.class, callback)
               .setParams("channelId", channelId)
               .setNoCache();
       HttpManager.addRequest(request, tag);
   }

    //创建频道
    public void createChannel(String tag,
                              String channelName,
                              String channelPass,
                              String channelId,
                              ApiCallback<CommonResult> callback){
        String url = getBaseChannelRequestUrl().append("createChannel").toString();

        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, CommonResult.class, callback)
                .setParams("channelName", channelName)
                .setParams("channelPass", channelPass)
                .setParams("chaCrePer", UserSessionManager.getInstance().getUserId())
                .setParams("channelId", channelId)
                .setNoCache();
        HttpManager.addRequest(request, tag);
    }

    //获取群组成员列表
    public void getMyChannelMember(String tag,String channelId,  ApiCallback<ChannelMemberListResult> callback){
        String url = getBaseChannelRequestUrl().append("myChannelMembers").toString();
        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, ChannelMemberListResult.class, callback)
                .setParams("channelId", channelId)
                .setParams("userId", UserSessionManager.getInstance().getUserId())
                .setNoCache();
        HttpManager.addRequest(request, tag);
    }

    //获取频道详情
    public void getChannelInfo(String tag, String channelRCId, ApiCallback<ChannelInfoResult> callback){
        String url = String.format("http://%s/jindun/%s/message/loadGroupsDetail/%s",
                Configs.SERVER_IP,Configs.API_VERSION,
                channelRCId);

        GsonRequest request = RequestFactory.newInstance()
                .create(url, ChannelInfoResult.class, callback)
                .setNoCache();
        HttpManager.addRequest(request, tag);
    }

    //修改频道状态
    public void modifyChannel(String tag, String channelId, String userId, String userStatus,
                              ApiCallback<CommonResult> callback){
        String url = getBaseChannelRequestUrl().append("channelStatus").toString();

        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, CommonResult.class, callback)
                .setParams("channelId", channelId)
                .setParams("userId", userId)
                .setParams("userStatus", userStatus)
                .setNoCache();
        HttpManager.addRequest(request, tag);
    }

    //退出频道
    public void quitChannel(String tag,  String userId, String channelId,
                              ApiCallback<CommonResult> callback){
        String url = String.format("http://%s/jindun/%s/message/deleteChannel",
                Configs.SERVER_IP,Configs.API_VERSION);

        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, CommonResult.class, callback)
                .setParams("channelId", channelId)
                .setParams("userId", userId)
                .setNoCache();
        HttpManager.addRequest(request, tag);
    }


    //我的频道列表
    public void getMyChannel(String tag,  ApiCallback<ChannelListResult> callback){
        String url = getBaseChannelRequestUrl().append("myChannelLine").toString();

        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, ChannelListResult.class, callback)
                .setParams("userId", UserSessionManager.getInstance().getUserId())
                .setNoCache();

        HttpManager.addRequest(request, tag);
    }

    //修改频道备注
    public void modifyChannelRemark(String tag,String channelId,String channelRemark,  ApiCallback<CommonResult> callback){
        String url = getBaseChannelRequestUrl().append("modifyChannel").toString();

        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, CommonResult.class, callback)
                .setParams("channelId", channelId)
                .setParams("channelName", channelRemark)
                .setParams("userId", UserSessionManager.getInstance().getUserId())
                .setNoCache();

        HttpManager.addRequest(request, tag);
    }

    //删除频道
    public void deleteChannel(String tag,String channelId, ApiCallback<CommonResult> callback){
        String url = getBaseChannelRequestUrl().append("deleteIntercom").toString();

        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, CommonResult.class, callback)
                .setParams("channelId", channelId)
                .setParams("userId", UserSessionManager.getInstance().getUserId())
                .setNoCache();

        HttpManager.addRequest(request, tag);
    }

    //添加用户进频道
    public void addChannelMember(String tag,  String channelId, String listLinkPer,
                            ApiCallback<ChannelInfoResult> callback){
        String url = getBaseChannelRequestUrl().append("addChannelMember").toString();

        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, ChannelInfoResult.class, callback)
                .setParams("channelId", channelId)
                .setParams("listLinkPer", listLinkPer)
                .setParams("userId", UserSessionManager.getInstance().getUserId())
                .setNoCache();
        HttpManager.addRequest(request, tag);
    }

    /**
     * 删除成员
     * @param tag
     * @param channelId
     * @param listLinkPer
     * @param callback
     */
    public void delChannelMember(String tag,  String channelId, String listLinkPer,
                                 ApiCallback<CommonResult> callback){
        String url = getBaseChannelRequestUrl().append("delChannelMember").toString();

        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, CommonResult.class, callback)
                .setParams("channelId", channelId)
                .setParams("listLinkPer", listLinkPer)
                .setParams("userId", UserSessionManager.getInstance().getUserId())
                .setNoCache();
        HttpManager.addRequest(request, tag);
    }

    private StringBuilder getBaseChannelRequestUrl(){
        return new StringBuilder(String.format("http://%s/jindun/%s/message/channel/",
                Configs.SERVER_IP,Configs.API_VERSION));
    }

    //从切换频道列表,进入频道
    public void joinChannel(String tag, String channelId, String channelPass,
                            ApiCallback<CommonResult> apiCallback) {
        String url = getBaseChannelRequestUrl().append("joinChannel").toString();

        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, CommonResult.class, apiCallback)
                .setParams("channelId", channelId)
                .setParams("userId", UserSessionManager.getInstance().getUserId())
                .setParams("channelPass", channelPass)
                .setNoCache();

        HttpManager.addRequest(request, tag);
    }

    //从切换频道列表,不输入密码进入频道
    public void enterChannel(String tag, String channelId,
                            ApiCallback<CommonResult> apiCallback) {
        String url = getBaseChannelRequestUrl().append("enterChannel").toString();

        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, CommonResult.class, apiCallback)
                .setParams("channelId", channelId)
                .setParams("userId", UserSessionManager.getInstance().getUserId())
                .setNoCache();

        HttpManager.addRequest(request, tag);
    }

    //获取对讲成员在线状态
    public void getCurrentChannelMemberStatus(String tag, String channelId,
                                              ApiCallback<ChannelMemberStatusResult> apiCallback){
        String url = getBaseChannelRequestUrl().append("status/").append(channelId).append("/").
                append(UserSessionManager.getInstance().getUserId()).toString();

        GsonRequest request = RequestFactory.newInstance()
                .create(url, ChannelMemberStatusResult.class, apiCallback)
                .setNoCache();

        HttpManager.addRequest(request, tag);
    }

    /**
     * 请求发言接口
     */
    public void controlMic(String tag, String channelId,
                                              ApiCallback<MicResult> apiCallback){
        String url = getBaseChannelRequestUrl().append("controlMic").toString();

        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, MicResult.class, apiCallback)
                .setParams("channelId", channelId)
                .setParams("userId", UserSessionManager.getInstance().getUserId())
                .setNoCache();

        HttpManager.addRequest(request, tag);
    }

    /**
     * 取消发言
     */
    public void cancelMic(String tag, String channelId,
                           ApiCallback<MicResult> apiCallback){
        String url = getBaseChannelRequestUrl().append("cancelMic").toString();

        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, MicResult.class, apiCallback)
                .setParams("channelId", channelId)
                .setParams("userId", UserSessionManager.getInstance().getUserId())
                .setNoCache();

        HttpManager.addRequest(request, tag);
    }

    /**
     * 结束发言
     */
    public void finishMic(String tag, String channelId,
                          ApiCallback<MicResult> apiCallback){
        String url = getBaseChannelRequestUrl().append("finishMic").toString();

        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, MicResult.class, apiCallback)
                .setParams("channelId", channelId)
                .setParams("userId", UserSessionManager.getInstance().getUserId())
                .setNoCache();

        HttpManager.addRequest(request, tag);
    }

    /**
     * 获取默认频道
     */
    public void getDefaultChannel(String tag,
                             ApiCallback<DefaultChannelResult> apiCallback) {
        String url = getBaseChannelRequestUrl().append("default").toString();

        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, DefaultChannelResult.class, apiCallback)
                .setParams("userId", UserSessionManager.getInstance().getUserId())
                .setNoCache();

        HttpManager.addRequest(request, tag);
    }

}
